package agent

import (
	"dub/define"
	"dub/define/grpcmodel"
	"dub/secrec"
)

type AgentController struct {
	AgentBaseController
}

func (a *AgentController) UpPwd() {
	jsons := make(map[string]interface{})
	jsons["result"] = false
	jsons["msg"] = "参数错误"

	for {
		opwd := a.GetString("opwd", "")
		npwd := a.GetString("npwd", "")
		if opwd == "" || npwd == "" {
			break
		}

		int_man := a.GetSession(define.Web_Session_Name_Agent_Login_Use)
		man_val, man_ok := int_man.(grpcmodel.ModelSecRpcAgentResLogin)
		if !man_ok {
			jsons["msg"] = "登录不合法"
			break
		}
		if man_val.GetModelSecRpcAgent().GetLoginPwd() != opwd {
			jsons["msg"] = "旧密码不正确"
			break
		}

		use_rpc := secrec.GetRpc(secrec.ConstServiceUseRpc)
		if use_rpc == nil {
			a.Log().Errorf("agent_controller.go UpPwd method not find use_rpc info GetRpc\n")
			break
		}
		args := &grpcmodel.ModelSecRpcAgentReqUpdate{
			IsAllUpdate: false,
			ModelSecRpcAgent: &grpcmodel.ModelSecRpcAgent{
				AgentId:  man_val.GetModelSecRpcAgent().GetAgentId(),
				LoginPwd: npwd,
			},
		}
		sec_agent := grpcmodel.NewISecRpcAgentClient(use_rpc.Conn())
		reply, err := sec_agent.Update(use_rpc.Context(), args)
		if err != nil {
			a.Log().Errorf("agent_controller.go UpPwd method s.dbRpc.Call SecAgentRpc.UpdateAgent err. %v\n", err)
			break
		}

		if reply.GetAgentId() > 0 {
			jsons["result"] = true
		}
		break
	}

	a.Data["json"] = jsons
	a.ServeJSON()
}
